<?php
namespace app\common\model;
use app\common\traits\Models;
use think\Model;
use think\facade\{
    App,Request,Config
};
class Deploy extends Model
{
    use Models;
    protected $readonly = [];
    protected $auto     = [];
    protected $insert   = [];
    protected $update   = [];
//获取器====================================================================================//
    public function getCreateTimeAttr($value)
    {
        return com_time_format($value);
    }
    public function getUpdateTimeAttr($value)
    {
        return com_time_format($value);
    }
    public function getTypeAttr($value)
    {
        return c("type.deploy_type",$value);
    }
    public function getAreaAttr($value)
    {
        return c("type.deploy_area",$value);
    }
    public function getGroupAttr($value)
    {
        return c("type.deploy_group",$value);
    }
/*关联模型=================================================================================*/

/*逻辑方法=================================================================================*/
    /**
    * 批量保存配置
    * @param array $data 配置数据
    * @return array
    */
    public function batchSave($data = []) {
        foreach ($data as $name => $value) {
           $status= $this::where(['name' => $name])->setField('value', $value);
           if ($status===false){
               session("js.name",$name);
               $this->error='系统错误，请稍候再试';
               return false;
           }
        }
        return true;
    }
    /**
     * 删除
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function del($id){
        if(!is_array($id)) $id = [$id];
        $id = implode(",",$id);
        $this::destroy($id);
        return true;
    }
    /**
     * 获取数据库中的配置列表
     * @param array $map_tmp 临时条件
     * @return array 配置数组
     * @internal param $map
     */
    public function lists($map=[])
    {
        //合并条件
        $object = $this::all(function ($query) use ($map) {
            $query->where($map)->field('type,name,value');
        });
        $config = [];
        if (is_object($object)) {
            $object = $object->toArray();
            foreach ($object as $value) {
                $config[ strtolower($value['name']) ] = $this->parse($value['type'], $value['value']);
            }
        }
        return $config;
    }

    /**
     * 根据配置类型解析配置
     * @param  integer $type 配置类型
     * @param  string  $value 配置值
     * @return array|string
     */
    private function parse($type, $value)
    {
        switch ($type) {
            case 2:
                $value=htmlspecialchars($value);
                break;
            case 3: //解析数组
                $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
                if (strpos($value, ':')) {
                    $value = [];
                    foreach ($array as $val) {
                        list($k, $v) = explode(':', $val);
                        $value[ $k ] = $v;
                    }
                } else {
                    $value = $array;
                }
                break;
            case 5:
                 $value=(int)$value;
                break;
        }
        return $value;
    }
}
